package deal;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import js.CsvExport;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import util.ConfigUtils;
public class DealDataOfHis implements Runnable{
	static Date d = new Date();
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
	static String dateNowStr = sdf.format(d);

	private static final String File_PATH = ConfigUtils.getInstance().getValue(
			"localbasePath");// 文件最终存放目录
	private static final String File_NAME = ConfigUtils.getInstance().getValue(
			"filename");// 存放文件名

	private static Logger logger=LogManager.getLogger(DealDataOfHis.class);
	private int flag;
	public DealDataOfHis(int flag){
		this.flag = flag;
	}

	@Override
	public void run() {
		logger.info("deal data start!");
		while(!CsvExport.exit) {
			Map<String, Object> dataMap = GetDataOfHis.dealData.poll();
			if (null != dataMap) {
				try{
					String id = String.valueOf(dataMap.get("ID")) ;
					String cityid = String.valueOf(dataMap.get("CITY_ID")) ;
					if (null!=id && !"".equals(id) && null!=cityid && !"".equals(cityid) && !"null".equals(id)) {
						//首先要判断该项目csv文件是否已创建
						String csvName = id+"_"+dateNowStr;
						String filePath = cityid+File.separator+id+File.separator+dateNowStr+File.separator+csvName ;
						File file = new File(File_PATH + File.separator + filePath + ".csv");
						if (file.exists()) {//csv文件存在，写入原有文件
							String testString = "";
							for(Object key:dataMap.keySet()){
								if (!"ID".equals(String.valueOf(key))) {
									testString+=dataMap.get(key)+",";
								}
							}
							testString=testString.substring(0,testString.length()-1);
							CsvExport.writeCsv(testString, filePath);
						}else {//文件不存在，新建文件
							CsvExport.createNewCsv2(csvName, dataMap, filePath);
						}
						CsvExport.increase();//写入一条数据 count+1
					}
				} catch (Exception e) {
					e.printStackTrace();
					logger.info("deal data error:"+e.getMessage());
				}
			}
		}
	}
}
